/**
 * Copyright 2010 Pavel Daniel Alexandru PFA
 * 
 * Licensed under the terms of the GNU Lesser General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License. 
 */
package com.kobol.webclient.client.main.view;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;
import com.kobol.webclient.client.Webclient;
import com.kobol.webclient.client.data.ResourceCentreDataSource;
import com.kobol.webclient.client.data.SalesDataSource;
import com.kobol.webclient.client.data.SettingsDataSource;
import com.kobol.webclient.client.main.presenter.MainPageSmartGwtPresenter;
import com.kobol.webclient.client.widgets.ApplicationMenu;
import com.kobol.webclient.client.widgets.Masthead;
import com.kobol.webclient.client.widgets.NavigationPane;
import com.kobol.webclient.client.widgets.NavigationPaneHeader;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;

/**
 * @author Pavel Alexandru
 * 
 */
public class MainPageSmartGwtView extends ViewImpl implements MainPageSmartGwtPresenter.MyView {

    // NORTH_HEIGHT = MASTHEAD_HEIGHT + APPLICATION_MENU_HEIGHT +
    // NAVIGATION_PANE_HEADER_HEIGHT
    private static final int NORTH_HEIGHT = 85;
    private static final int DEFAULT_MENU_WIDTH = 70;
    private static final String DEFAULT_MARGIN = "0px";

    private final Masthead masthead;
    private final ApplicationMenu applicationMenu;
    private final NavigationPaneHeader navigationPaneHeader;
    private final NavigationPane navigationPane;

    private VLayout panel;
    private HLayout northLayout;
    private HLayout southLayout;
    private VLayout westLayout;

    @Inject
    public MainPageSmartGwtView(Masthead masthead, ApplicationMenu applicationMenu,
	    NavigationPaneHeader navigationPaneHeader, NavigationPane navigationPane) {
	this.masthead = masthead;
	this.applicationMenu = applicationMenu;
	this.navigationPaneHeader = navigationPaneHeader;
	this.navigationPane = navigationPane;

	// get rid of scroll bars, and clear out the window's built-in margin,
	// because we want to take advantage of the entire client area
	Window.enableScrolling(false);
	Window.setMargin(DEFAULT_MARGIN);

	// initialise the main layout container
	panel = new VLayout();
	panel.setWidth100();
	panel.setHeight100();

	// initialise the North layout container
	northLayout = new HLayout();
	northLayout.setHeight(NORTH_HEIGHT);

	initApplicationMenu();

	// initialise the nested layout container
	VLayout vLayout = new VLayout();
	vLayout.addMember(this.masthead);
	vLayout.addMember(this.applicationMenu);
	vLayout.addMember(this.navigationPaneHeader);

	// add the nested layout container to the North layout container
	northLayout.addMember(vLayout);

	initNavigationPane();

	// initialise the West layout container
	westLayout = this.navigationPane;

	// initialise the South layout container
	southLayout = new HLayout();

	// add the North and South layout containers to the main layout
	// container
	panel.addMember(northLayout);
	panel.addMember(southLayout);
    }

    @Override
    public Widget asWidget() {
	return panel;
    }

    @Override
    public void setInSlot(Object slot, Widget content) {

	Log.debug("setInSlot()");

	if (slot == MainPageSmartGwtPresenter.TYPE_SetContextAreaContent) {
	    if (content != null) {
		southLayout.setMembers(westLayout, (VLayout) content);
	    }
	} else {
	    super.setInSlot(slot, content);
	}
    }

    private void initApplicationMenu() {
	applicationMenu.addMenu(Webclient.getConstants().NewActivityMenuName(), DEFAULT_MENU_WIDTH, Webclient
		.getConstants().NewActivityMenuItemNames(), null);
	applicationMenu.addMenu(Webclient.getConstants().NewRecordMenuName(), DEFAULT_MENU_WIDTH, Webclient
		.getConstants().NewRecordMenuItemNames(), null);
	Menu goToMenu = applicationMenu.addMenu(Webclient.getConstants().GoToMenuName(), DEFAULT_MENU_WIDTH - 30);
	applicationMenu.addSubMenu(goToMenu, Webclient.getConstants().SalesMenuItemName(), Webclient.getConstants()
		.SalesMenuItemNames(), null);
	applicationMenu.addSubMenu(goToMenu, Webclient.getConstants().SettingsMenuItemName(), Webclient.getConstants()
		.SettingsMenuItemNames(), null);
	applicationMenu.addSubMenu(goToMenu, Webclient.getConstants().ResourceCentreMenuItemName(), Webclient
		.getConstants().ResourceCentreMenuItemNames(), null);
	applicationMenu.addMenu(Webclient.getConstants().ToolsMenuName(), DEFAULT_MENU_WIDTH - 30, Webclient
		.getConstants().ToolsMenuItemNames(), null);
	applicationMenu.addMenu(Webclient.getConstants().HelpMenuName(), DEFAULT_MENU_WIDTH - 30, Webclient
		.getConstants().HelpMenuItemNames(), null);
    }

    private void initNavigationPane() {
	navigationPane.add(Webclient.getConstants().SalesStackSectionName(), SalesDataSource.getInstance());
	navigationPane.add(Webclient.getConstants().SettingsStackSectionName(), SettingsDataSource.getInstance());
	navigationPane.add(Webclient.getConstants().ResourceCentreStackSectionName(),
		ResourceCentreDataSource.getInstance());
    }

    public ApplicationMenu getApplicationMenu() {
	return applicationMenu;
    }

    public NavigationPaneHeader getNavigationPaneHeader() {
	return navigationPaneHeader;
    }

    public NavigationPane getNavigationPane() {
	return navigationPane;
    }
}
